// pages/community/community.js
import request from '../../utils/request'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoList: [],
    refresh: true,
    offset: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getVideoList();
  },

  async getVideoList() {
    const offset = this.data.offset
    const res = await request('/video/timeline/recommend', {
      timestamp: Date.now(),
      offset
    })

    if (offset > 0) {
      this.setData({
        videoList: [...this.data.videoList, ...res.datas]
      })
      return
    }

    this.setData({
      videoList: res.datas
    })
  },

  refreshstart() {
    this.setData({
      refresh: true
    })
  },

  async refreshVideoList() {
    this.setData({
      offset: 0
    })
    await this.getVideoList();
    this.setData({
      refresh: false
    })
  },

  goVideoPlayer(e) {
    wx.setStorageSync('url', e.currentTarget.dataset.video.data.urlInfo.url)
    wx.navigateTo({
      url: `../video-player/video-player`,
    })
  },

  scrollToBottom() {
    this.setData({
      offset: this.data.offset + 8
    })
    this.getVideoList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})